home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / serial / serialdriverarbitration / serialdriverarbitration.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.1 KB  |  156 lines

  1. /*
  2.     File:        SerialDriverArbitration.c
  3.  
  4.     Contains:    This is an example of how to correctly arbitrate the serial ports.  It's taken
  5.                  from tech note DV 11 - Opening the Serial Ports.
  6.  
  7.     Written by: Brian Bechtel.  Updated by Lenae Rowland.    
  8.  
  9.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/7/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 12/5/96        Lenae Rowland    Updated
  22.                 10/7/93        Brian Bechtel    Created
  23.                 
  24.  
  25. */
  26. // Comments beginning with -LR- are changes made to
  27. // accomodate new Universal Headers
  28.  
  29. // -LR-  Changed GestaltEqu.h to Gestalt.h
  30. #include <Gestalt.h>
  31. // -LR-  Changed SysEqu.h to LowMem.h
  32. #include <LowMem.h>
  33. #include <Devices.h>
  34.  
  35. #include <stdio.h>
  36. #include <StringCompare.h>
  37.  
  38. #define gestaltArbitorAttr                 'arb '
  39. #define gestaltSerialArbitrationExists    0
  40. #define dRAMBased        0x0040
  41. #define dOpened            0x0020
  42.  
  43. // This is a list of drivers used for test purposes in this code
  44. Str255    gDriverNames[] = 
  45.     {
  46.         "\p.NoDrvr",     // non-existant driver
  47.         "\p.AOut",         // serial port A output
  48.         "\p.BIn",         // serial port B input 
  49.         "\p.BOut",         // serial port B output
  50.         "\p.AppleCD",     // CD-ROM output
  51.         "\p.MPP",         // AppleTalk
  52.         "\p.ASYC00",    // a hard disk driver
  53.         "\p.AIn"         // serial port A input
  54.     };
  55.  
  56.  
  57. Boolean SerialArbitrationExists(void);
  58. Boolean DriverIsOpen(StringPtr driverName);
  59. Boolean CRMInstalled(void);
  60.  
  61. // Test Gestalt to see if serial arbitration exists
  62. // on this machine
  63. Boolean SerialArbitrationExists(void)
  64. {
  65.     long    response;
  66.     OSErr    err;
  67.     
  68.     err = Gestalt(gestaltArbitorAttr, &response);
  69.     if (err)
  70.         return false;
  71.     return ((response >> gestaltSerialArbitrationExists) & 1);
  72. }
  73.  
  74.  
  75. Boolean DriverIsOpen(StringPtr driverName)
  76. {
  77.     Boolean canOpen = false;
  78.     Boolean match = false;
  79.     short    index = 0;
  80.     short    count;
  81.     DCtlHandle    dceHandle;
  82.     StringPtr    namePtr;
  83.     DCtlHandle    *theUnitTable;
  84.     
  85.     // -LR- changed this to a low memory accessor function
  86.     count = LMGetUnitTableEntryCount();
  87.     
  88.     // -LR- changed this to a low memory accessor function
  89.     theUnitTable = (DCtlHandle*)LMGetUTableBase();
  90.     
  91.     while ( !match && (index < count)) {
  92.         // get handle to a device control entry
  93.         dceHandle = theUnitTable[index];
  94.         
  95.         if (dceHandle) {
  96.             if (!( (**dceHandle).dCtlFlags & dRAMBased) )
  97.                 // RAM based drivers have a handle to their driver
  98.                 namePtr = (StringPtr)(**dceHandle).dCtlDriver+18;
  99.             else
  100.                 // ROM based drivers have a pointer to their driver
  101.                 namePtr = (StringPtr) (*(DCtlPtr)dceHandle).dCtlDriver+18;
  102.  
  103.             // not case sensitive, diacritical marks count
  104.             if (RelString(driverName, namePtr, false, true) == 0) {
  105.                 match = true;
  106.                 canOpen = ((**dceHandle).dCtlFlags & dOpened) ? true : false;
  107.             }
  108.         }
  109.         index++;
  110.     }
  111.     return canOpen;
  112. }
  113.  
  114. Boolean CRMInstalled(void)
  115. {
  116.     long    response;
  117.     OSErr    err;
  118.     
  119.     err = Gestalt(gestaltCRMAttr, &response);
  120.     if (err)
  121.         return false;
  122.     return ((response >> gestaltCRMPresent) & 1);
  123. }
  124.  
  125. #define TEST_CALL
  126. void main()
  127. {
  128.     OSErr    err = noErr;
  129.     SInt16    i;
  130.     SInt16    numDrivers;
  131.     
  132.     printf("Test of serial port stuff\n");
  133.     if (CRMInstalled())
  134.         printf("Communication Resource Manager is installed.\n");
  135.     if (SerialArbitrationExists())
  136.         printf("Serial Arbitration exists\n");
  137.         
  138. #ifdef TEST_CALL
  139.     numDrivers = sizeof(gDriverNames) / sizeof(Str255);
  140.     for (i = 0; i < numDrivers; i++)
  141.         printf("DriverIsOpen(%#s)\treturned %s\n", 
  142.                 gDriverNames[i], DriverIsOpen(gDriverNames[i])?"True ":"False");
  143. #endif
  144.  
  145. #ifdef TEST_OPEN
  146.     SInt16    refNum;
  147.  
  148.     if (!DriverIsOpen("\p.AIn"))
  149.         err = OpenDriver("\p.AIn", &refNum);
  150.     if (err)
  151.         printf("OpenDriver returned %d\n", err);
  152.     err = CloseDriver(refNum);
  153.     if (err)
  154.         printf("CloseDriver returned %d\n", err);
  155. #endif
  156. }